﻿@using Microsoft.AspNetCore.Mvc.Localization
@using WeihanLi.Extensions
@model IPagerModel
@inject IViewLocalizer Localizer

@if (Model.TotalCount > 0)
{
    if (Model.PageCount < 2 && Model.PagingDisplayMode == PagingDisplayMode.IfNeeded)
    {
    }
    else
    {
        int offset = (Model.GroupSize) / 2;
        int pageStart = Model.PageNumber - offset, pageEnd = Model.PageNumber + offset, curPage = 0;
        <div style="text-align: center">
            <nav>
                <ul class="pagination">
                    @if (Model.HasPreviousPage)
                    {
                        <li>
                            <a href='@Model.OnPageChange(Model.PageNumber - 1)' aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                    }
                    else
                    {
                        <li class="disabled">
                            <a href="javascript:void(0)" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                    }
                    @if (pageStart <= 1 && pageEnd >= Model.PageCount)
                    {
                        for (var i = 0; i <= Model.GroupSize; i++)
                        {
                            curPage = pageStart + i;
                            if ((curPage >= 1) && (curPage <= Model.PageCount))
                            {
                                if (curPage == Model.PageNumber)
                                {
                                    <li class="active">
                                        <a href="javascript:void(0)">
                                            @Model.PageNumber<span class="sr-only">(current)</span>
                                        </a>
                                    </li>
                                }
                                else
                                {
                                    <li>
                                        <a href='@Model.OnPageChange(curPage)'>
                                            @curPage <span class="sr-only"> @curPage </span>
                                        </a>
                                    </li>
                                }
                            }
                        }
                    }
                    else
                    {
                        var temp = 0;
                        if (pageStart > 1)
                        {
                            <li>
                                <a href='@Model.OnPageChange(1)'>1<span class="sr-only">1</span></a>
                            </li>
                            <li>
                                <a href='@Model.OnPageChange(2)'>2<span class="sr-only">1</span></a>
                            </li>
                            <li class="disbaled">
                                <a href="javascript:void(0)">...<span class="sr-only">...</span></a>
                            </li>
                            temp = Model.PageNumber - offset / 2;
                            curPage = temp;
                            for (var i = 0; i <= offset / 2 + 1; i++)
                            {
                                curPage = temp + i;
                                if ((curPage >= 1) && (curPage <= Model.PageCount))
                                {
                                    if (curPage == Model.PageNumber)
                                    {
                                        <li class="active">
                                            <a href="javascript:void(0)">
                                                @Model.PageNumber<span class="sr-only">(current)</span>
                                            </a>
                                        </li>
                                        break;
                                    }
                                    else
                                    {
                                        <li>
                                            <a href='@Model.OnPageChange(curPage)'>
                                                @curPage <span class="sr-only"> @curPage </span>
                                            </a>
                                        </li>
                                    }
                                }
                            }
                        }
                        else
                        {
                            for (var i = 0; i <= offset; i++)
                            {
                                curPage = pageStart + i;
                                if ((curPage >= 1) && (curPage <= Model.PageCount))
                                {
                                    if (curPage == Model.PageNumber)
                                    {
                                        <li class="active">
                                            <a href="javascript:void(0)">
                                                @Model.PageNumber<span class="sr-only">(current)</span>
                                            </a>
                                        </li>
                                        break;
                                    }
                                    else
                                    {
                                        <li>
                                            <a href='@Model.OnPageChange(curPage)'>
                                                @curPage <span class="sr-only"> @curPage </span>
                                            </a>
                                        </li>
                                    }
                                }
                            }
                        }
                        if (pageEnd < Model.PageCount)
                        {
                            temp = Model.PageNumber + 1;
                            curPage = temp;
                            for (var i = 0; i <= offset / 2 && curPage <= Model.PageCount; i++)
                            {
                                curPage = temp + i;
                                if ((curPage >= 1) && (curPage <= Model.PageCount))
                                {
                                    <li>
                                        <a href='@Model.OnPageChange(curPage)'>
                                            @curPage <span class="sr-only"> @curPage </span>
                                        </a>
                                    </li>
                                }
                            }
                            <li class="disbaled">
                                <a href="javascript:void(0)">...<span class="sr-only">...</span></a>
                            </li>
                            <li>
                                <a href='@Model.OnPageChange(Model.PageCount - 1)'>
                                    @(Model.PageCount - 1)<span class="sr-only">@(Model.PageCount - 1)</span>
                                </a>
                            </li>
                            <li>
                                <a href='@Model.OnPageChange(Model.PageCount)'>
                                    @Model.PageCount<span class="sr-only">@Model.PageCount</span>
                                </a>
                            </li>
                        }
                        else
                        {
                            curPage = Model.PageNumber;
                            for (var i = 0; i <= offset && curPage <= Model.PageCount; i++)
                            {
                                curPage++;
                                if ((curPage >= 1) && (curPage <= Model.PageCount))
                                {
                                    if (curPage == Model.PageNumber)
                                    {
                                        <li class="active">
                                            <a href="javascript:void(0)">
                                                @Model.PageNumber<span class="sr-only">(current)</span>
                                            </a>
                                        </li>
                                        break;
                                    }
                                    else
                                    {
                                        <li>
                                            <a href='@Model.OnPageChange(curPage)'>
                                                @curPage <span class="sr-only"> @curPage </span>
                                            </a>
                                        </li>
                                    }
                                }
                            }
                        }
                    }

                    @if (Model.HasNextPage)
                    {
                        <li>
                            <a href='@Model.OnPageChange(Model.PageNumber + 1)' aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    }
                    else
                    {
                        <li class="disabled">
                            <a href="javascript:void(0)" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    }
                </ul>
            </nav>
            <div>
                <span>@(Localizer["PagerInfo"].Value.FormatWith(Model.PageSize, Model.PageCount, Model.TotalCount))</span>
            </div>
        </div>
    }
}
else
{
    <div class="nodata-tip">
        @Localizer["noDataTip"]
    </div>
}